<?php

namespace app\admin\controller\questions;

use app\admin\controller\Controllers;
use app\admin\service\questions\Question as QuestionService;
use app\admin\model\questions\Question as QuestionModels;
use app\admin\service\questions\ExaminationType as ExaminationTypeService;
use app\admin\service\questions\QuestionCategory as QuestionCategoryService;

class Question extends Controllers
{
    /**
     * 显示资源列表
     * @param QuestionService $service
     *
     * @return \think\Response
     */
    public function index( QuestionService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\questions\Question.index');
        $list = $service->indexPaginate($get);
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total()
        ]);
        return $this->fetch('questions/question/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(ExaminationTypeService $examinationTypeService, QuestionCategoryService $questionCategoryService)
    {
        $this->assign([
            'examination_type' => $examinationTypeService->getType(1),
            'examination_section' => $examinationTypeService->getType(2),
            'examination_discipline' => $examinationTypeService->getType(3),
            'examination_area' => $examinationTypeService->getType(4),
            'question_category' => $questionCategoryService->getLists(),
            'config' => config('field.question')
        ]);
        return $this->fetch('questions/question/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(QuestionService $service)
    {
        $post = $this->request->only(['title','recommend','examination_type','examination_section','examination_discipline','examination_area','cover','category'], 'post');
        $this->validate($post, 'app\admin\validate\questions\Question.Create');
        $questionid = $service->create($post);
        if ( false === $questionid ) {
            return failed('新增失败');
        }
        return success();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  QuestionModels $model
     * @return \think\Response
     */
    public function edit( QuestionModels $model, ExaminationTypeService $examinationTypeService, QuestionCategoryService $questionCategoryService )
    {
        $this->assign([
            'data' => $model,
            'examination_type' => $examinationTypeService->getType(1),
            'examination_section' => $examinationTypeService->getType(2),
            'examination_discipline' => $examinationTypeService->getType(3),
            'examination_area' => $examinationTypeService->getType(4),
            'question_category' => $questionCategoryService->getLists(),
            'config' => config('field.question')
        ]);
        return $this->fetch('questions/question/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(QuestionModels $model, QuestionService $service)
    {
        $put = $this->request->only(['title','recommend','examination_type','examination_section','examination_discipline','examination_area','cover','category'], 'put');
        $this->validate($put, 'app\admin\validate\questions\Question.Update');
        $update = $service->save($model, $put);
        if ( false === $update ) {
            return failed('编辑失败');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param QuestionService $service
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(QuestionService $service, int $id)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\questions\Question.delete');
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 批量删除
     * @param QuestionService $service
     * @return \think\Response
     */
    public function destory(QuestionService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\questions\Question.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
